"""
    Some basic unittests
    Requires a local database to be setup.
"""

from pgate.gate import create_app
import unittest
import operator
from flask import Flask, Response
import types
from testbase import PartnerGateBaseTestCase


class PartnerGateTestCase(PartnerGateBaseTestCase):
    """Unittest class to be used by runner."""

    def setUp(self):
        """Setup for the tests"""
        self.app = create_app().test_client()
        #insert some test data in database
        #setup database
        #clear tables

    def tearDown(self):
        """Teardown for the tests"""
        #close database
        pass

    def test_non_mdf5_hash(self):
        """Test a 32 char url that is not a valid md5"""
        response = self.app.get("/12435/zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz")
        self.assert200(response)
        self.assertEqual(response.data, "0")

    def test_callback_feature(self):
        """Test the callback feature of partner gate."""
        response = self.app.get("/12435/e018e2d49783e212c38c33f69fed6c01" \
                                "?callback=alert")
        self.assert200(response)
        self.assertTrue("alert" in response.data)

    def test_bogus(self):
        """Test a bogus url"""
        response = self.app.get("/12435/23423432")
        self.assert200(response)
        self.assertEqual(response.data, "0")

    def test_bogus2(self):
        """Test a bogus url"""
        response = self.app.get("/doug")
        self.assert200(response)
        self.assertEqual(response.data, "0")

    def test_same_timestamp(self):
        """Test everytime you call with a specific url the same
           timestamp is returned."""
        first = self.assert_url_response("50001",
                                         "e018e2d49783e212c38c33f69fed6c01")
        second = self.assert_url_response("50001",
                                          "e018e2d49783e212c38c33f69fed6c01")
        self.assertEqual(first, second)

    def test_total(self):
        """Test the total functionality"""
        total = self.assert_total_response("50001")
        self.assertEqual(total, "1")

    def test_full_cycle(self):
        """Test calling a url and getting the total after"""
        self.test_same_timestamp()
        self.test_total()

    def replay_log(self):
        """Replay a log file."""
        pass

if __name__ == "__main__":
    unittest.main()
